package core.communication;

import java.net.BindException;

import org.jboss.remoting.InvalidConfigurationException;
import org.jboss.remoting.InvokerLocator;
import org.jboss.remoting.transport.Connector;

public class Server {
	private String locatorURI = null;
	private IInvocationHandler invocationHandler = null;
	private Connector connector = null;

	public Server(String locatorURI, IInvocationHandler iHandler) throws BindException {
		try {
			this.locatorURI = locatorURI;
			InvokerLocator invokerLocator = null;
			invokerLocator = new InvokerLocator("socket://" + locatorURI);
			connector = new Connector(invokerLocator);
			connector.create();
			this.invocationHandler = iHandler;
			connector.addInvocationHandler(locatorURI, invocationHandler);
			if (!connector.isStarted())
				connector.start();
		} catch (BindException e) {
			throw e;
		} catch (InvalidConfigurationException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void shutdownServer() {
		if (connector != null) {
			connector.stop();
		}
	}

	public String getLocatorURI() {
		return this.locatorURI;
	}
}
